﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConfeccionesJR.Entites;
using System.Data.Objects;

namespace ConfeccionesJR.Repository
{
    public class DetalleProformaRepository:MasterDataAccess ,IDetalleProformaRepository
    {
        public List<DETALLEPROFORMA> GetAllFromDetalleProformaByProformaId(int id)
        {
            var query = from c in Context.DETALLEPROFORMAs
                            .Include("PRODUCTO")
                            
                        where c.proformaId.Equals(id)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.ToList();
        }


        public void DeleteDetalleProfomra(int idProducto, int idProforma)
        {
            var existe = Context.DETALLEPROFORMAs
                .Where(p => p.productoId.Equals(idProducto) &&
                 p.proformaId.Equals(idProforma)).SingleOrDefault();
            if (existe != null)
            {
                Context.DETALLEPROFORMAs.Remove(existe);
                Context.SaveChanges();
            }
        }


        public DETALLEPROFORMA GetProdFromDetProformaByProdIdAndProformaId(int idProd, int idProforma)
        {
            var query = from c in Context.DETALLEPROFORMAs
                        where c.productoId.Equals(idProd) && c.proformaId.Equals(idProforma)
                        select c;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);

            return query.SingleOrDefault();
        }


        public List<DETALLEPROFORMA> GetFromDetalleProformaByProductoDescAndProformaId(string descripcion, int id)
        {
            if (descripcion != "")
            {
                var query = from c in Context.DETALLEPROFORMAs.Include("PRODUCTO")
                            where c.Producto.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            && c.proformaId.Equals(id)
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
            else
            {
                var query = from c in Context.DETALLEPROFORMAs.Include("PRODUCTO")
                            where c.proformaId.Equals(id)
                            select c;

                ObjectContext.Refresh(RefreshMode.StoreWins, query);

                return query.ToList();
            }
        }
    }
}
